﻿/**************************************************************************
创建日期:	2019/7/21 21:10:53     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)    ZhCun.cn

描	述：
记	录：
***************************************************************************/
using System.Collections.Generic;
using ZhCun.DbCore.Cores;
using ZhCun.RightsManage.BLL;
using ZhCun.RightsManage.DBModel;
using ZhCun.Utils;
using ZhCun.Utils.Helpers;
using ZhCun.WebUtils.DB;
using ZhCun.WebUtils.VModel;

namespace ZhCun.RightsManage.AL
{
    public class Role_UpdateOrder
    {
        public string RoleMenuId { set; get; }

        public int OrderNo { set; get; }
    }

    public class Role_UpdateIsExpand
    {
        public string RoleMenuId { set; get; }

        public bool IsExpand{ set; get; }
    }

    public class RoleBLL : BLLCrudBase<ArgGetList, Sys_Role>
    {
        protected override bool CheckSaveArg(Sys_Role model, bool isAdd, out string msg)
        {
            msg = null;
            if (model.RoleName.IsEmpty())
            {
                msg = "角色名称不能为空";
                return false;
            }
            return true;
        }

        public override ApiResult Update(Sys_Role model)
        {
            if (StringHelper.IsNullOrWhiteSpace(model.RoleName))
            {
                return this.RetErr("角色名称不能为空");
            }
            P_RoleSave p = new P_RoleSave();
            p.Arg_RoleId = model.Id;
            p.Arg_RoleName = model.RoleName;
            p.Arg_MenuIds = model.Menus;
            p.Arg_IsSysRole = 0;
            DB.ExecProcedure(p).ExecProc();
            if (p.OutCode == 0)
            {
                return this.RetOK();
            }
            return this.RetErr(p.OutMsg);
        }

        public ApiResult UpdateRoleMenuOrder(Role_UpdateOrder arg)
        {
            Sys_RoleMenu t = new Sys_RoleMenu();
            t.Id = arg.RoleMenuId;
            t.MenuOrder = arg.OrderNo;
            var r = DB.Update(t);
            if (r.RowCount == 1)
            {
                return this.RetOK();
            }
            else
            {
                return this.RetErr("更新失败");
            }
        }

        public ApiResult UpdateIsExpand(Role_UpdateIsExpand arg)
        {
            Sys_RoleMenu t = new Sys_RoleMenu();
            t.Id = arg.RoleMenuId;
            t.MenuExpand = arg.IsExpand;
            var r = DB.Update(t);
            if (r.RowCount == 1)
            {
                return this.RetOK();
            }
            else
            {
                return this.RetErr("更新失败");
            }
        }

        public override ApiResult Add(Sys_Role model)
        {
            if (StringHelper.IsNullOrWhiteSpace(model.RoleName))
            {
                return this.RetErr("角色名称不能为空");
            }
            P_RoleSave p = new P_RoleSave();
            p.Arg_RoleName = model.RoleName;
            p.Arg_MenuIds = model.Menus;
            DB.ExecProcedure(p).ExecProc();
            if (p.OutCode == 0)
            {
                return this.RetOK();
            }
            return this.RetErr(p.OutMsg);
        }

        protected override ExecResult GetDelResult(string roleId)
        {
            DB.Delete<Sys_RoleMenu>(s => s.RoleId == roleId);
            return DB.Delete<Sys_Role>(s => s.Id == roleId);
        }

        protected override void SetQueryOption(ArgGetList arg, QueryCondition<Sys_Role> query)
        {
            query.OrderBy(s => s.AddTime);
            if (!string.IsNullOrWhiteSpace(arg.searchValue))
            {
                var likeStr = $"%{arg.searchValue}%";
                query.WhereAnd(s =>
                    s.WEx_Like(s.RoleName, likeStr)
                );
            }
        }

        /// <summary>
        /// 得到所有菜单，并选中已分配的菜单
        /// </summary>
        public ApiResultList<LayuiTreeModel> GetLayuiMenuTree(string roleId)
        {
            List<LayuiTreeModel> treeList = new List<LayuiTreeModel>();
            P_RoleMenuGet p = new P_RoleMenuGet();
            p.Arg_RoleId = roleId;
            var r = DB.ExecProcedure(p);
            var rMenus = r.ToList<PV_SysRoleMenu>();

            foreach (var item in rMenus)
            {
                if (string.IsNullOrWhiteSpace(item.PId))
                {
                    LayuiTreeModel subTree = new LayuiTreeModel();
                    subTree.id = item.Id;
                    subTree.title = item.MenuName;
                    subTree.spread = true;
                    subTree.@checked = item.IsRoleMenu == 0 ? false : true;
                    LoadLayuiTreeMenu(subTree, rMenus);
                    treeList.Add(subTree);
                }
            }
            return this.RetOK(treeList, treeList.Count);
        }

        protected void LoadLayuiTreeMenu(LayuiTreeModel tree, List<PV_SysRoleMenu> menuList)
        {
            foreach (var item in menuList)
            {
                if (item.PId == tree.id)
                {
                    if (tree.children == null) tree.children = new List<LayuiTreeModel>();
                    LayuiTreeModel subTree = new LayuiTreeModel();
                    subTree.id = item.Id;
                    subTree.title = item.MenuName;
                    if (item.IsRoleMenu == 0)
                    {
                        subTree.@checked = false;
                    }
                    else
                    {
                        tree.@checked = false; //子节点选中了，父节点不选中，layui插件有这个毛病
                        subTree.@checked = true;
                    }
                    tree.children.Add(subTree);
                    LoadLayuiTreeMenu(subTree, menuList);
                }
            }
        }
        /// <summary>
        /// 用于配置角色的排序等信息
        /// </summary>
        public ApiResultList<VSys_RoleMenu> GetRoleMenus(string roleId)
        {
            var query = DB.CreateQuery<VSys_RoleMenu>();
            query.WhereAnd(s => s.RoleId == roleId);
            query.OrderBy(s => s.MenuLevel).OrderBy(s => s.MenuOrder);
            var rList = DB.Query<VSys_RoleMenu>(query).ToList(true);
            return this.RetOK(rList, rList.Count);
        }
    }
}